﻿package	birdry.ui	{
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.display.BitmapData;
	import flash.display.Bitmap;
	import flash.geom.Rectangle;
	import flash.geom.Point;
	import flash.events.*;
	
	public class Scratch_ extends MovieClip	{
		var sMC:MovieClip = new MovieClip();
		var alphaNum:Number;
		var Stage:Object;
		var This:MovieClip;
		
		public function Scratch_(MC:MovieClip, pixel:uint, alphaNum:Number):void	{
			this.alphaNum = alphaNum;
			this.Stage = Stage;
			this.This = MC;
			
			var bd1:BitmapData = new BitmapData(MC.width, MC.height);
			var bd2:BitmapData;
			bd1.draw(MC);
			
			var _w:uint = Math.ceil(MC.width/pixel);
			var _h:uint = Math.ceil(MC.height/pixel);
			MC.visible = false;
			
			var _x:uint = 0;
			var _y:uint = 0;
			var _z:uint = 0;
			
			for(var i=0; i<_w; i++)	{
				for(var u=0; u<_h; u++)	{
					bd2 = new BitmapData(pixel, pixel);
					bd2.copyPixels(bd1, new Rectangle(_x, _y, _x+pixel, _y+pixel), new Point(0, 0));
					var bm:Bitmap = new Bitmap(bd2);
					sMC["bm"+_z] = new MovieClip();
					sMC["bm"+_z] = sMC["bm"+_z].addChild(bm);
					sMC["bm"+_z].x = _x;
					sMC["bm"+_z].y = _y;
					
					sMC.addChild(sMC["bm"+_z]);
					_y += pixel;
					_z++;
				}
				_x += pixel;
				_y = 0;
			}
			sMC.addEventListener(MouseEvent.MOUSE_DOWN, downHandler);
			sMC.x = MC.x;
			sMC.y = MC.y;
			MC.parent.addChild(sMC);
		}
		
		private function downHandler(e:MouseEvent):void	{
			addEventListener(Event.ENTER_FRAME, moveHandler);
			This.stage.addEventListener(MouseEvent.MOUSE_UP, upHandler);
		}
		private function upHandler(e:MouseEvent):void	{
			removeEventListener(Event.ENTER_FRAME, moveHandler);
			This.stage.removeEventListener(MouseEvent.MOUSE_UP, upHandler);
		}
		private function moveHandler(e:Event):void	{
			
			for(var i:String in sMC)	{
				if(sMC[i].hitTestPoint(this.mouseX, this.mouseY, false))	{
					sMC[i].alpha += -alphaNum;
				}
			}
		}
	}
}
